use std::future::Future;
use std::ops::{Deref, DerefMut};
use std::string::ToString;
use std::time::Duration;

use lazy_static::lazy_static;
use log::info;
use tokio::net::TcpListener;

use crate::config::ftlog_init;
use crate::cus_redis::mini_redis_server::TOKIO_RUNTIME;
use crate::ROOT_APP_CONFIG;

mod mini_redis_server;
mod mini_redis_client;
mod tools;
mod mini_redis_data_base;
mod mini_redis_connection;
mod mini_redis_frame;

// pub mod mini_redis_client;
fn log_init(){
    ftlog_init(&ROOT_APP_CONFIG).unwrap();
}
fn tokio_main(fut: impl Future<Output = ()> + Sized) {
    ftlog_init(&ROOT_APP_CONFIG).unwrap();
    TOKIO_RUNTIME.block_on(fut);
}

lazy_static!{
    static ref IP_PORT: String = "127.0.0.1:16379".to_string();
}

#[test]
fn redis_server() {
    tokio_main(async {
        let listener = TcpListener::bind(IP_PORT.deref()).await.unwrap();
        mini_redis::server::run(listener, async {
            tokio::signal::ctrl_c().await.unwrap();
            info!("!!!mini-redis-server shut down now!!!");
        })
        .await
        .unwrap();
    });
}
#[test]
fn redis_client() {
    tokio_main(async {
        let mut client = mini_redis::client::connect(IP_PORT.deref()).await.unwrap();
        client.set_expires("key", "value".into(),Duration::from_secs(3)).await.unwrap();
        let info = client.get("key").await.unwrap();
        info!("{:?}", info);
        tokio::time::sleep(Duration::from_secs(3)).await;
        let info = client.get("key").await.unwrap();
        info!("{:?}", info);


    });
}
